package com.sdry.web.controller.llm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.service.llm.UserReportService;
import com.sdry.service.lqy.WeeklyProductionReportService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 
 * @Title:UserReportController
 * @Package com.sdry.web.controller.llm
 * @author llm
 * @date 2019年1月6日
 * @version 1.0
 */

@Controller
@RequestMapping(value="/reportMana2")
public class UserReportController {

	@Resource
	private UserReportService userReportService;
	@Resource
	private WeeklyProductionReportService weeklyproductionreport;
	
	/**
	 * 从数据库查数据存入人员报表
	 * @Title:UserReport   
	 * @param time
	 * @param uid
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/userReport")
	public void UserReport(String time, Long uid, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		if(null != time && !"".equals(time)){
			map.put("time", time);
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			String date = sdf.format(new Date());
			map.put("time", date);
		}
		if(null != uid && !"".equals(uid)){
			map.put("uid", uid);
		}else{
			map.put("uid", 0L);
		}
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		List<com.sdry.model.llm.UserReport> list = userReportService.listAllProductionByTimeAndTeam(map);
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 0);
		ResponseUtil.write(response, jsonObject);
	}
	
	
	/**
	 * 从人员报表查数据
	 * @Title:UserReport   
	 * @param time
	 * @param uid
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listUserReportByTimeAndUid")
	public void listUserReportByTimeAndUid(String time, Long uid, HttpServletResponse response) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String date = sdf.format(new Date());
		Map<String, Object> map = new HashMap<>();
		if(null != time && !"".equals(time)){
			map.put("time", time);
		}else{
			map.put("time", date);
		}
		if(null != uid && !"".equals(uid)){
			map.put("uid", uid);
		}else{
			map.put("uid", 0L);
		}
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		List<com.sdry.model.llm.UserReport> list = new ArrayList<>();
		
		if(null != uid && uid > 0){
			list = userReportService.listUserReportByTimeAndUid(map);
		}
		
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		
		Calendar calendar=Calendar.getInstance();
		int day=calendar.get(Calendar.DAY_OF_MONTH);//当前是当月的第几天
		List<com.sdry.model.llm.UserReport> list2 = new ArrayList<>();
		if(list.size() > 0){
			//如果查当月数据
			if(date.equals(map.get("time"))){
				for(int i = 0; i < list.size(); i ++){
					if(i < day || i == (list.size()-1) || i == (list.size()-2)){
						list2.add(list.get(i));
					}
				}
				jsonArray = JSONArray.fromObject(list2, jsonConfig);
			}
		}
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", 0);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 更新人员报表
	 * @Title:renewalUserReport   
	 * @param dateTime
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/renewalUserReport")
	public void renewalUserReport(String dateTime, HttpServletResponse response)throws Exception{
		String month = null;
		if(null == dateTime || "".equals(dateTime)){
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
			month = format.format(date);
		}else{
			month = dateTime;
		}
		Integer isSuccess = userReportService.renewalUserReport(month);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * @Title:job   
	 * @param dateTime
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/job")
	public void job(String dateTime, HttpServletResponse response) throws Exception{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		if(null != dateTime && !"".equals(dateTime) ){
			time = dateTime;
		}
		Integer userReportByToday = userReportService.getUserReportByToday(time);
		ResponseUtil.write(response, userReportByToday);
	}
	
	@RequestMapping(value="/job2")
	public void job2(String dateTime, HttpServletResponse response) throws Exception{
		/*Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		if(null != dateTime && !"".equals(dateTime) ){
			time = dateTime;
		}*/
		Integer userReportByToday = weeklyproductionreport.updatacomprehening("");
		ResponseUtil.write(response, userReportByToday);
	}
	
}
